//
//  ZMCityListVC.m
//  testDemo
//
//  Created by 赵林瑞 on 16/5/12.
//  Copyright © 2016年 RZOL. All rights reserved.
//

#import "ZMCityListVC.h"

#import <CoreLocation/CoreLocation.h>
#import "MTzkCityListVC.h"

@interface ZMCityListVC ()<CLLocationManagerDelegate,choiseCityDelegate>
{
    IBOutlet UIButton *_clickBtn;
    
    NSString * locationCityName;
    MTzkCityListVC * mtTest;
}

/** 位置管理者 */
@property (nonatomic ,strong) CLLocationManager * locationManager;

/** 地理编码 */
@property (nonatomic, strong) CLGeocoder *geoC;

@end

@implementation ZMCityListVC

- (CLLocationManager *)locationManager
{
    if (!_locationManager) {
        _locationManager = [[CLLocationManager alloc]init];
        _locationManager.delegate = self;
        
        if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
            [_locationManager requestWhenInUseAuthorization];
        }
    }
    return _locationManager;
}

- (CLGeocoder *)geoC
{
    if (!_geoC) {
        _geoC = [[CLGeocoder alloc] init];
    }
    return _geoC;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    mtTest = [[MTzkCityListVC alloc]init];
    mtTest.delegate = self;
    
    NSUserDefaults * setting = [NSUserDefaults standardUserDefaults];
    NSString * key = [NSString stringWithFormat:@"cityLocation"];
    NSString * cityName = [setting objectForKey:key];
    
    if ([_clickBtn.titleLabel.text isEqualToString:@"城市定位"])
    {// 开始定位
        [self.locationManager startUpdatingLocation];
        
        _clickBtn.titleLabel.text = cityName;
    }
}


-(void)theChoiseCity:(NSString *)choiseCity
{
    NSUserDefaults * setting = [NSUserDefaults standardUserDefaults];
    NSString * key = [NSString stringWithFormat:@"cityLocation"];
    [setting setObject:choiseCity forKey:key];
    [setting synchronize];
    
    _clickBtn.titleLabel.text = choiseCity;
}

//设置定位信息
-(void)setUpLocation
{
    CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
    // 2.判断用户是否授权
    if (status == kCLAuthorizationStatusNotDetermined) {
        NSLog(@"等待用户授权");
    }else if (status == kCLAuthorizationStatusAuthorizedAlways ||
              status == kCLAuthorizationStatusAuthorizedWhenInUse)
    {
        NSLog(@"授权成功");
        
        _locationManager.distanceFilter = 100;
        _locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
    }else
    {
        NSLog(@"授权失败");
    }
}

#pragma mark CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    [self.locationManager stopUpdatingLocation];
    CLLocation *currLocation=[locations lastObject];
    double theLatitude = currLocation.coordinate.latitude;
    double theLongitude = currLocation.coordinate.longitude;
    
    CLLocation *loc = [[CLLocation alloc] initWithLatitude:theLatitude longitude:theLongitude];
    [self.geoC reverseGeocodeLocation:loc completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        
        if(error == nil)
        {
            [placemarks enumerateObjectsUsingBlock:^(CLPlacemark * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                locationCityName = obj.locality;
                
            }];
        }else
        {
            locationCityName = @"北京市";
        }
        
        NSUserDefaults * setting = [NSUserDefaults standardUserDefaults];
        NSString * key = [NSString stringWithFormat:@"cityLocation"];
        [setting setObject:locationCityName forKey:key];
        [setting synchronize];
        
        _clickBtn.titleLabel.text = locationCityName;
        
    }];
}


- (IBAction)click:(id)sender {
    mtTest.navigationItem.title   = @"城市定位";
    [self.navigationController pushViewController:mtTest animated:YES];
}

@end
